package com.agile.major.web.rest;

import com.agile.major.domain.MajorQcbook;
import com.agile.major.service.MajorQcbookService;
import com.agile.major.web.rest.util.HeaderUtil;
import com.agile.major.web.rest.util.PaginationUtil;
import com.codahale.metrics.annotation.Timed;
import io.github.jhipster.web.util.ResponseUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing MajorQcbook.
 */
@RestController
@RequestMapping("/api")
public class MajorQcbookResource {

    private final Logger log = LoggerFactory.getLogger(MajorQcbookResource.class);

    private static final String ENTITY_NAME = "majorQcbook";

    private final MajorQcbookService majorQcbookService;

    public MajorQcbookResource(MajorQcbookService majorQcbookService) {
        this.majorQcbookService = majorQcbookService;
    }

    /**
     * POST  /major-qcbooks : Create a new majorQcbook.
     *
     * @param majorQcbook the majorQcbook to create
     * @return the ResponseEntity with status 201 (Created) and with body the new majorQcbook, or with status 400 (Bad Request) if the majorQcbook has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/major-qcbooks")
    @Timed
    public ResponseEntity<MajorQcbook> createMajorQcbook(@RequestBody MajorQcbook majorQcbook) throws URISyntaxException {
        log.debug("REST request to save MajorQcbook : {}", majorQcbook);
        if (majorQcbook.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new majorQcbook cannot already have an ID")).body(null);
        }
        MajorQcbook result = majorQcbookService.save(majorQcbook);
        return ResponseEntity.created(new URI("/api/major-qcbooks/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /major-qcbooks : Updates an existing majorQcbook.
     *
     * @param majorQcbook the majorQcbook to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated majorQcbook,
     * or with status 400 (Bad Request) if the majorQcbook is not valid,
     * or with status 500 (Internal Server Error) if the majorQcbook couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/major-qcbooks")
    @Timed
    public ResponseEntity<MajorQcbook> updateMajorQcbook(@RequestBody MajorQcbook majorQcbook) throws URISyntaxException {
        log.debug("REST request to update MajorQcbook : {}", majorQcbook);
        if (majorQcbook.getId() == null) {
            return createMajorQcbook(majorQcbook);
        }
        MajorQcbook result = majorQcbookService.save(majorQcbook);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, majorQcbook.getId().toString()))
            .body(result);
    }

    /**
     * GET  /major-qcbooks : get all the majorQcbooks.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of majorQcbooks in body
     */
    @GetMapping("/major-qcbooks")
    @Timed
    public ResponseEntity<List<MajorQcbook>> getAllMajorQcbooks(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of MajorQcbooks");
        Page<MajorQcbook> page = majorQcbookService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/major-qcbooks");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /major-qcbooks/:id : get the "id" majorQcbook.
     *
     * @param id the id of the majorQcbook to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the majorQcbook, or with status 404 (Not Found)
     */
    @GetMapping("/major-qcbooks/{id}")
    @Timed
    public ResponseEntity<MajorQcbook> getMajorQcbook(@PathVariable Long id) {
        log.debug("REST request to get MajorQcbook : {}", id);
        MajorQcbook majorQcbook = majorQcbookService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(majorQcbook));
    }

    /**
     * DELETE  /major-qcbooks/:id : delete the "id" majorQcbook.
     *
     * @param id the id of the majorQcbook to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/major-qcbooks/{id}")
    @Timed
    public ResponseEntity<Void> deleteMajorQcbook(@PathVariable Long id) {
        log.debug("REST request to delete MajorQcbook : {}", id);
        majorQcbookService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
    @GetMapping("/major-qcbooks/byMajor/{id}")
    @Timed
    public ResponseEntity<Page<MajorQcbook>> getQcBooksByMajor(@PathVariable Long id,@ApiParam Pageable pageable) {
        log.debug("REST request to get MajorQcbook : {}", id);
        Page<MajorQcbook> page = majorQcbookService.getQcBookByMajor(id,pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/major-qcbooks");
        return new ResponseEntity<>(page, headers, HttpStatus.OK);
    }
}
